/**
 *	@brief	名木橋用デバッグソース　常駐
 */
#ifdef PM_DEBUG

#include "common.h"

#include "system/lib_pack.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "fld_bmp.h"
#include "fieldmap.h"
#include "fieldsys.h"
#include "fieldmap_work.h"
#include "system/fontproc.h"
#include "system/window.h"
#include "system/numfont.h"
#include "system/arc_util.h"
#include "system/snd_tool.h"
#include "system/snd_system.h"

#include "d_nagi.h"
#include "msgdata/msg_debug_nagi.h"
#include "msgdata/msg.naix"

#include "system/touchpanel_draw.h"
#include "field_light.h"
#include "field_glb_state.h"
#include "talk_msg.h"

#include "script.h"

#include "encount_effect.h"
#include "system/wipe.h"


#include "application/mictest.h"
#include "d_nagi_local.h"


#define APPLICATION_FADE_SYNC	(16)

//----------------------------------------------------------------------------
/**
 *	@brief	アプリケーション用ワーク削除
 *
 *	@param	D_NAGI_APPLICATION_WORK* p_wk	削除するワーク
 *
 *	@return	none
 */
//-----------------------------------------------------------------------------
static void APPLICATION_FreeWork( D_NAGI_APPLICATION_WORK* p_wk )
{
	if( p_wk->p_param != NULL ) {
		sys_FreeMemoryEz( p_wk->p_param );
	}
	sys_FreeMemoryEz( p_wk );
}

//----------------------------------------------------------------------------
/**
 *	@brief	アプリケーション動作タスク
 *
 *	@param	TCB_PTR p_tcb
 *	@param	* p_work
 *
 *	@return
 */
//-----------------------------------------------------------------------------
void DebugNagi_CallApplication( TCB_PTR p_tcb, void * p_work )
{
	enum {
		SEQ_FADEOUT_START,
		SEQ_FADEOUT_WAIT,
		SEQ_PROC_START,
		SEQ_PROC_WAIT,
		SEQ_FLDPRCRET_START,
		SEQ_FLDPRCRET_WAIT,
		SEQ_FADEIN_START,
		SEQ_FADEIN_WAIT,
		SEQ_END,
	};

	D_NAGI_APPLICATION_WORK *p_wk = p_work;

	switch( p_wk->seq ){
	case SEQ_FADEOUT_START:
		p_wk->bgm_no	= Snd_NowBgmNoGet();
		WIPE_SYS_Start(WIPE_PATTERN_WMS,WIPE_TYPE_FADEOUT,WIPE_TYPE_FADEOUT,
				WIPE_FADE_BLACK, APPLICATION_FADE_SYNC,1,HEAPID_FIELD
				);
		p_wk->seq = SEQ_FADEOUT_WAIT;
		break;

	case SEQ_FADEOUT_WAIT:
		if(WIPE_SYS_EndCheck()){
			p_wk->seq = SEQ_PROC_START;
		}
		break;

	case SEQ_PROC_START:
		p_wk->SetProcFunction( p_wk->p_param );
		FieldSystemProc_SeqHoldEnd();
		p_wk->seq = SEQ_PROC_WAIT;
		break;

	case SEQ_PROC_WAIT:
		if( !FieldEvent_Cmd_WaitSubProcEnd( p_wk->fsys ) ){
			p_wk->seq	= SEQ_FLDPRCRET_START;
		}
		break;

	case SEQ_FLDPRCRET_START:
		//フィールドマッププロセス復帰
		FieldEvent_Cmd_SetMapProc(p_wk->fsys);
		p_wk->seq	= SEQ_FLDPRCRET_WAIT;
		break;

	case SEQ_FLDPRCRET_WAIT:
		//フィールドプロセス開始終了待ち
		if(FieldEvent_Cmd_WaitMapProcStart(p_wk->fsys)){
			p_wk->seq	= SEQ_FADEIN_START;
		}
		break;

	case SEQ_FADEIN_START:
		//ブラックインリクエスト
		WIPE_SYS_Start(WIPE_PATTERN_WMS,WIPE_TYPE_FADEIN,WIPE_TYPE_FADEIN,
				WIPE_FADE_BLACK, APPLICATION_FADE_SYNC,1,HEAPID_FIELD
				);
		Snd_BgmPlay(p_wk->bgm_no);
		p_wk->seq	= SEQ_FADEIN_WAIT;
		break;

	case SEQ_FADEIN_WAIT:
		if(WIPE_SYS_EndCheck()){
			p_wk->seq	= SEQ_END;
		}
		break;

	case SEQ_END:
		NAGIHASHI_PRINT("フィールドに復帰\n");
		//	TCBとワークを削除
		APPLICATION_FreeWork( p_wk );
		TCB_Delete( p_tcb );
		break;
	}
}

void DebugNagi_FactoryTask( TCB_PTR p_tcb, void * p_work )
{
	D_FACTORY_WORK * wk = p_work;

	switch( wk->seq ){
	case 0:
		WIPE_SYS_Start(
			WIPE_PATTERN_WMS, WIPE_TYPE_FADEOUT, WIPE_TYPE_FADEOUT,
			WIPE_FADE_BLACK, APPLICATION_FADE_SYNC, 1, HEAPID_FIELD );
		wk->seq++;
		break;

	case 1:
		if (WIPE_SYS_EndCheck() ){
			FieldSystemProc_SeqHoldEnd();
			wk->func( wk );
			wk->seq++;
		}
		break;

	case 2:
		if( !FieldEvent_Cmd_WaitSubProcEnd( wk->fsys ) ){
			FieldEvent_Cmd_SetMapProc( wk->fsys );
			wk->seq++;
		}
		break;

	case 3:
		if( FieldEvent_Cmd_WaitMapProcStart(wk->fsys) ){
			WIPE_SYS_Start(
				WIPE_PATTERN_WMS, WIPE_TYPE_FADEIN, WIPE_TYPE_FADEIN,
				WIPE_FADE_BLACK, APPLICATION_FADE_SYNC, 1, HEAPID_FIELD );
			wk->seq++;
		}
		break;

	case 4:
		if( WIPE_SYS_EndCheck() ){
			sys_FreeMemoryEz( wk );
			TCB_Delete( p_tcb );
		}
		break;
	}
}

//-----------------------------------------------------------------------------
/*
	伝説デモ

	d_nakahiro.cがオーバーレイ化されているので、間借りします。
	2008/12/11 by nakahiro
*/
//-----------------------------------------------------------------------------
/*
#include "demo/legend_demo.h"

BOOL DEBUG_LegendDemoMain( GMEVENT_CONTROL * event );
BOOL DEBUG_LegendDemoMain( GMEVENT_CONTROL * event )
{
	FIELDSYS_WORK * fsys;
	D_LD_WORK * wk;

	fsys = FieldEvent_GetFieldSysWork( event );
	wk   = FieldEvent_GetSpecialWork( event );

	switch( wk->seq ){
	case 0:
		if( WIPE_SYS_EndCheck() ){
			FieldSystemProc_SeqHoldEnd();
			FieldOBJSys_MoveStopAll( fsys->fldobjsys );
//			WazaOshie_ProcSet( fsys , &wk->dat );
			wk->dat = LegendDemo_SetProc( fsys, fsys->glst_data, wk->mode, wk->time, HEAPID_BASE_DEBUG );
			wk->dat->scene = wk->scene;
			wk->dat->debug = wk->play_mode;
			wk->seq = 1;
		}
		break;

	case 1:
		if( !( FieldEvent_Cmd_WaitSubProcEnd(fsys) ) ) {
			FieldEvent_Cmd_SetMapProc( fsys );
			wk->seq = 2;
		}
		break;

	case 2:
		if( FieldEvent_Cmd_WaitMapProcStart( fsys ) ){
			FieldFadeWipeSet( FLD_DISP_BRIGHT_BLACKIN );
			wk->seq = 3;
		}
		break;

	case 3:
		if( WIPE_SYS_EndCheck() ){
			sys_FreeMemoryEz( wk->dat );
			sys_FreeMemoryEz( wk );
			FieldOBJSys_MoveStopAllClear( fsys->fldobjsys );
			return TRUE;
		}
	}
	return FALSE;
}
*/

//-----------------------------------------------------------------------------
/*
	メールボックス

	d_nakahiro.cがオーバーレイ化されているので、間借りします。
	2009/01/31 by nakahiro
*/
//-----------------------------------------------------------------------------
/*
#include "application/mailbox.h"

BOOL DEBUG_MailBoxMain( GMEVENT_CONTROL * event );
BOOL DEBUG_MailBoxMain( GMEVENT_CONTROL * event )
{
	FIELDSYS_WORK * fsys;
	DEBUG_MB_WORK * wk;

	fsys = FieldEvent_GetFieldSysWork( event );
	wk   = FieldEvent_GetSpecialWork( event );

	switch( wk->seq ){
	case 0:
		if( WIPE_SYS_EndCheck() ){
			FieldSystemProc_SeqHoldEnd();
			FieldOBJSys_MoveStopAll( fsys->fldobjsys );
			wk->dat = FieldMailBox_SetProc( fsys, HEAPID_BASE_DEBUG );
			wk->seq = 1;
		}
		break;

	case 1:
		if( !( FieldEvent_Cmd_WaitSubProcEnd(fsys) ) ) {
			FieldEvent_Cmd_SetMapProc( fsys );
			wk->seq = 2;
		}
		break;

	case 2:
		if( FieldEvent_Cmd_WaitMapProcStart( fsys ) ){
			FieldFadeWipeSet( FLD_DISP_BRIGHT_BLACKIN );
			wk->seq = 3;
		}
		break;

	case 3:
		if( WIPE_SYS_EndCheck() ){
			sys_FreeMemoryEz( wk->dat );
			sys_FreeMemoryEz( wk );
			FieldOBJSys_MoveStopAllClear( fsys->fldobjsys );
			return TRUE;
		}
	}
	return FALSE;
}
*/

//#include "application/frontier_monitor.h"
/*
BOOL DEBUG_FrontierMonitorEvent( GMEVENT_CONTROL * event );
BOOL DEBUG_FrontierMonitorEvent( GMEVENT_CONTROL * event )
{
	FIELDSYS_WORK * fsys;
	D_FM_WORK * wk;

	fsys = FieldEvent_GetFieldSysWork( event );
	wk   = FieldEvent_GetSpecialWork( event );

	switch( wk->seq ){
	case 0:
		if( WIPE_SYS_EndCheck() ){
			FieldSystemProc_SeqHoldEnd();
			FieldOBJSys_MoveStopAll( fsys->fldobjsys );
//			WazaOshie_ProcSet( fsys , &wk->dat );
			wk->dat = FrontierMonitor_SetProc( fsys, wk->type, wk->fr_no, wk->monsno, HEAPID_BASE_DEBUG );
			wk->seq = 1;
		}
		break;

	case 1:
		if( !( FieldEvent_Cmd_WaitSubProcEnd(fsys) ) ) {
			FieldEvent_Cmd_SetMapProc( fsys );
			wk->seq = 2;
		}
		break;

	case 2:
		if( FieldEvent_Cmd_WaitMapProcStart( fsys ) ){
			FieldFadeWipeSet( FLD_DISP_BRIGHT_BLACKIN );
			wk->seq = 3;
		}
		break;

	case 3:
		if( WIPE_SYS_EndCheck() ){
			sys_FreeMemoryEz( wk->dat );
			sys_FreeMemoryEz( wk );
			FieldOBJSys_MoveStopAllClear( fsys->fldobjsys );
			return TRUE;
		}
	}
	return FALSE;
}
*/
/*
BOOL DEBUG_BattleTowerAppEvent( GMEVENT_CONTROL * event );
BOOL DEBUG_BattleTowerAppEvent( GMEVENT_CONTROL * event )
{
	FIELDSYS_WORK * fsys;
	D_BTAPP_WORK * wk;

	fsys = FieldEvent_GetFieldSysWork( event );
	wk   = FieldEvent_GetSpecialWork( event );

	switch( wk->seq ){
	case 0:
		if( WIPE_SYS_EndCheck() ){
			FieldSystemProc_SeqHoldEnd();
			FieldOBJSys_MoveStopAll( fsys->fldobjsys );
			BTowerApp_SetProc( fsys, wk->dat );
//			WazaOshie_ProcSet( fsys , &wk->dat );
//			wk->dat = FrontierMonitor_SetProc( fsys, wk->type, wk->fr_no, wk->monsno, HEAPID_BASE_DEBUG );
			wk->seq = 1;
		}
		break;

	case 1:
		if( !( FieldEvent_Cmd_WaitSubProcEnd(fsys) ) ) {
			FieldEvent_Cmd_SetMapProc( fsys );
			wk->seq = 2;
		}
		break;

	case 2:
		if( FieldEvent_Cmd_WaitMapProcStart( fsys ) ){
			FieldFadeWipeSet( FLD_DISP_BRIGHT_BLACKIN );
			wk->seq = 3;
		}
		break;

	case 3:
		if( WIPE_SYS_EndCheck() ){
			sys_FreeMemoryEz( wk->dat );
			sys_FreeMemoryEz( wk );
			FieldOBJSys_MoveStopAllClear( fsys->fldobjsys );
			return TRUE;
		}
	}
	return FALSE;
}
*/

#endif	//PM_DEBUG
